#include "xftp_stor.h"

#include <event2/event.h>
#include <event2/bufferevent.h>

#include <iostream>

using namespace std;

XFtpStor::XFtpStor() {
}

XFtpStor::~XFtpStor() {
}


void XFtpStor::Parse(std::string type, std::string msg) {
    // 取出文件名
    int pos = msg.rfind(" ");
    std::string filename = msg.substr(pos, msg.size() - pos - 2);

    std::string path = cmdTask->rootDir;
    path += cmdTask->curDir;
    path += filename;

    fp = fopen(path.c_str(), "wb");
    if (fp) {
        // 连接数据通道
        ConnectPort();
        // 发送开始接收文件的指令
        ResCmd("125 File OK\r\n");

        // 触发读取事件
        bufferevent_trigger(bev, EV_READ, 0);
    } else {
        ResCmd("450 file open failed.\r\n");
    }
}

void XFtpStor::Read(struct bufferevent *bev) {
    if (!fp) return;

    for (;;) {
        int len = bufferevent_read(bev, buf, sizeof(buf));
        if (len <= 0) {
            return;
        }

        int size = fwrite(buf, 1, len, fp);
        cout << "<" << len << ":" << size << ">" << flush << endl;
    }
}

void XFtpStor::Event(struct bufferevent *bev, short what) {
    // 如果对方网络断掉,或者机器死机有可能收不到BEV_EVENT_EOF数据
    if (what & (BEV_EVENT_EOF | BEV_EVENT_ERROR | BEV_EVENT_TIMEOUT)) {
        cout << "XFtpStor BEV_EVENT_EOF | BEV_EVENT_ERROR | BEV_EVENT_TIMEOUT" << endl;
        ResCmd("226 Transfer complete\r\n");
        Close();
    } else if (what & BEV_EVENT_CONNECTED) {
        cout << "XFtpStor BEV_EVENT_CONNECTED" << endl; 
    }
}

